import pymysql


def main():
    conn = pymysql.connect(host='192.168.84.89', port=3306,
                           user='root', password='Zhjai1@131waaA23',
                           db='hrs', charset='utf8',
                           cursorclass=pymysql.cursors.DictCursor)  # 字典型游标 DictCursor
    try:
        with conn.cursor() as cursor:
            # cursor.execute('select dno,dname,dloc from tb_dept')
            # 查所有行 fetchall()  查指定的行fetchmany()  查一行 fetchone()
            # print(cursor.fetchone())
            # print(cursor.fetchmany(3))
            # print(cursor.fetchall())

            # 打印出所有的元组
            # for row in cursor.fetchall():
            #     print(f'部门编号:{row[0]}')
            #     print(f'部门名称:{row[1]}')
            #     print(f'部门所在地:{row[2]}')
            #     print('*' * 20)
            cursor.execute('select dno as no,dname as name,dloc as location from tb_dept')
            for row in cursor.fetchall():
                print('no:', row['no'], end='\t')
                print('name:', row['name'], end='\t')
                print('localtion:', row['location'])


    except pymysql.MySQLError as error:
        print(error)
    finally:
        # 关闭连接，释放资源
        conn.close()


if __name__ == '__main__':
    main()
